import {
	asyncRoutes,
	constantRoutes
} from '@/router'

const state = {
	routes: constantRoutes // 所有人默认拥有静态路由
}

const mutations = {
	// newRoutes可以认为是 用户登录 通过权限所得到的动态路由的部分
	setRoutes(state, newRoutes) {
		state.routes = [...constantRoutes, ...newRoutes]
	}
}

const actions = {
	filterRoutes(context, menus) {
		const routes = []
		
		menus.forEach(key => {
			routes.push(...asyncRoutes.filter(item => item.name === key))
		})
		context.commit('setRoutes', routes)
		return routes
	}
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}